package com.zerdoor.controller;

import com.octo.captcha.service.CaptchaServiceException;
import com.zerdoor.model.UserInfo;
import com.zerdoor.service.LoginService;
import com.zerdoor.util.yz.CaptchaServiceSingleton;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Project: acgplat
 * Module ID: 010
 * Comments: 登陆管理
 * JDK version used: JDK1.7
 * Namespace: controller
 * Author：xiaobo
 * Create Date：2014-11-24
 * Modified By：   <修改人中文名或拼音缩写>
 * Modified Date:  <修改日期，格式:YYYY-MM-DD>
 * Why & What is modified  <修改原因描述>
 * Version:                  <版本号>
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;
    private Logger logger = LoggerFactory.getLogger(LoginController.class);

    @RequestMapping("/index")
    public String index(Model model){
        return "/login";
    }
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> login(@RequestBody Map<String, String> model, HttpServletRequest request) {
        //logger.info("登录验证码：" + model.get("verifcode"));
        Map<String, Object> map = new HashMap<String, Object>(2);
//        if (equVerifCode((String) model.get("verifcode"), request)) {
            UserInfo tuser = loginService.getTuserByName(model.get("name"));
            if (null != tuser) {
                if (loginService.verifyLogin(tuser, model, request)) {
                    //登陆成功引用shire
//                    UsernamePasswordToken token = new UsernamePasswordToken(tuser.getUserName(), tuser
//                            .getPsd().toString());
//                    Subject subject = SecurityUtils.getSubject();
//                    subject.login(token);
                    map.put("result", true);
                } else {
                    map.put("result", false);
                    map.put("msg", "psdImg");
                }
            } else {
                map.put("msg", "userImg");
            }
//        } else {
//            map.put("msg", "vercImg");
//        }
        return map;
    }

    /**
     * 验证码验证
     *
     * @param verifcode
     * @param request
     * @return
     */
    public boolean equVerifCode(String verifcode, HttpServletRequest request) {
        Boolean isResponseCorrect = Boolean.FALSE;
        String captchaId = request.getSession().getId();
        try {
            isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId, verifcode);
            if (isResponseCorrect) {
                return true;
            } else {
                return false;
            }
        } catch (CaptchaServiceException e) {
            return false;
        }
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ModelAndView logout(HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView();
        request.getSession().setAttribute("curManager", null);
        modelAndView.setViewName("/login");
        return modelAndView;
    }

    /**
     * 无权限
     * @param model
     * @return
     */
    @RequestMapping("/authority")
    public String authority(Model model){
        return "/authority";
    }

    @RequestMapping("/welcome")
    public String welcome(Model model){
        return "/welcome";
    }

}
